home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / SciTex / examples / phong / display.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.7 KB  |  282 lines

  1. /******************************************************************************
  2.  *  display.c
  3.  ******************************************************************************
  4.  *
  5.  *  Purpose:
  6.  *      Descriptional text.
  7.  *
  8.  *  Authors:
  9.  *      Michael Teschner and Christian Henn
  10.  *
  11.  *  Note:
  12.  *      None.
  13.  *  
  14.  *  Revisions:
  15.  *      10.11.93    micha    Created file.
  16.  *
  17.  ******************************************************************************
  18.  *
  19.  *  COPYRIGHT (C)                  1992, 1993, 1994
  20.  *
  21.  *  BY  CHRISTIAN HENN             M.E. MUELLER-INSTITUTE FOR MICROSCOPY (MIM)
  22.  *      HENN@COMP.BIOZ.UNIBAS.CH   CH-4056 BASEL, SWITZERLAND   
  23.  *
  24.  *  AND MICHAEL WALDHERR-TESCHNER  SILICON GRAPHICS INDUSTRIES (SGI)
  25.  *      MICHA@BASEL.SGI.COM        CH-4125 RIEHEN, SWITZERLAND
  26.  *
  27.  ******************************************************************************
  28.  *
  29.  *  PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS DOCU-
  30.  *  MENTATION FOR THE PURPOSE OF RESEARCH, DEVELOPMENT AND EDUCATION IS HEREBY
  31.  *  GRANTED FREE OF CHARGE, SUBJECT TO THE FOLLOWING RESTRICTIONS:
  32.  *
  33.  *  THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  34.  *  IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF DESIGN,
  35.  *  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A
  36.  *  COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  37.  *
  38.  *  IN NO EVENT SHALL SILICON GRAPHICS OR THE M.E. MUELLER-INSTITUTE BE LIABLE
  39.  *  FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  40.  *  OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  41.  *  WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  42.  *  LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  43.  *  THIS SOFTWARE.
  44.  *
  45.  ******************************************************************************
  46.  */
  47. /******************************************************************************
  48.  * INCLUDE_FILES
  49.  ******************************************************************************
  50.  */
  51.  
  52. #include <stdio.h>
  53. #include <gl/gl.h>
  54. #include "trackball.h"
  55.  
  56. /******************************************************************************
  57.  * PRIVATE_PARAMETERS
  58.  ******************************************************************************
  59.  */
  60.  
  61. float sphere[4] = { 0.0, 0.0, 0.0, 5.0 };
  62. float material[20][21];
  63. float light[10][10];
  64. float envmat[4][4];    /* rotation matrix for the environment map */
  65. int shade_flag;
  66.  
  67. gr_init()
  68. {
  69.  
  70.   /* define the material properties */
  71.   material[0][0]  = EMISSION;
  72.   material[0][1]  =  0.0;
  73.   material[0][2]  =  0.0;
  74.   material[0][3]  =  0.0;
  75.   material[0][4]  = SPECULAR;
  76.   material[0][5]  =  1.0;
  77.   material[0][6]  =  1.0;
  78.   material[0][7]  =  1.0;
  79.   material[0][8]  = DIFFUSE;
  80.   material[0][9]  =  0.2;
  81.   material[0][10] =  0.2;
  82.   material[0][11] =  0.2;
  83.   material[0][12] = AMBIENT;
  84.   material[0][13] =  0.1;
  85.   material[0][14] =  0.1;
  86.   material[0][15] =  0.1;
  87.   material[0][16] = SHININESS;
  88.   material[0][17] = 96.0;
  89.   material[0][18] = ALPHA;
  90.   material[0][19] =  1.0;
  91.   material[0][20] = LMNULL;
  92.  
  93.   /* define the light properties */
  94.  
  95.   light[0][0]     = LCOLOR;        /* the first light source */
  96.   light[0][1]     =  1.0;
  97.   light[0][2]     =  1.0;
  98.   light[0][3]     =  1.0;
  99.   light[0][4]     = POSITION;
  100.   light[0][5]     = 10.0;
  101.   light[0][6]     = 10.0;
  102.   light[0][7]     = 10.0;
  103. /* >>> set from 1.0 (local) to 0.0 (infinite) to produce wrong
  104. environment mapping */  
  105.   light[0][8]     =  1.0;        /* local light source */
  106.   light[0][9]     = LMNULL;
  107.  
  108.   light[1][0]     = LCOLOR;             /* the second light source */
  109.   light[1][1]     =  0.1;
  110.   light[1][2]     =  0.1;
  111.   light[1][3]     =  0.1;
  112.   light[1][4]     = POSITION;
  113.   light[1][5]     = -2.0;
  114.   light[1][6]     = -0.7;
  115.   light[1][7]     =  0.0;
  116.   light[1][8]     =  0.0;               /* infinite light source */
  117.   light[1][9]     = LMNULL;
  118.  
  119.   light[2][0]     = LCOLOR;             /* the third light source */
  120.   light[2][1]     =  0.3;
  121.   light[2][2]     =  0.3;
  122.   light[2][3]     =  0.3;
  123.   light[2][4]     = POSITION;
  124.   light[2][5]     = -1.0;
  125.   light[2][6]     = -0.7;
  126.   light[2][7]     =  0.0;
  127.   light[2][8]     =  0.0;               /* infinite light source */
  128.   light[2][9]     = LMNULL;
  129.  
  130.   light[3][0]     = LCOLOR;             /* the fourth light source */
  131.   light[3][1]     =  0.3;
  132.   light[3][2]     =  0.3;
  133.   light[3][3]     =  0.3;
  134.   light[3][4]     = POSITION;
  135.   light[3][5]     = -0.7;
  136.   light[3][6]     = -1.0;
  137.   light[3][7]     =  0.0;
  138.   light[3][8]     =  0.0;               /* infinite light source */
  139.   light[3][9]     = LMNULL;
  140.  
  141.   light[4][0]     = LCOLOR;             /* the fifth light source */
  142.   light[4][1]     =  0.1;
  143.   light[4][2]     =  0.1;
  144.   light[4][3]     =  0.1;
  145.   light[4][4]     = POSITION;
  146.   light[4][5]     = -0.7;
  147.   light[4][6]     = -2.0;
  148.   light[4][7]     =  0.0;
  149.   light[4][8]     =  0.0;               /* infinite light source */
  150.   light[4][9]     = LMNULL;
  151.  
  152.   light[5][0]     = LCOLOR;             /* the sixth light source */
  153.   light[5][1]     =  0.3;
  154.   light[5][2]     =  0.3;
  155.   light[5][3]     =  0.3;
  156.   light[5][4]     = POSITION;
  157.   light[5][5]     = -2.0;
  158.   light[5][6]     = -2.0;
  159.   light[5][7]     =  0.0;
  160.   light[5][8]     =  0.0;               /* infinite light source */
  161.   light[5][9]     = LMNULL;
  162.  
  163.   /* define the lighting setup */
  164.   lmdef(DEFMATERIAL,1,0,material[0]);
  165.   lmdef(DEFLIGHT,   1,0,light[0]);
  166.   lmdef(DEFLIGHT,   2,0,light[1]);
  167.   lmdef(DEFLIGHT,   3,0,light[2]);
  168.   lmdef(DEFLIGHT,   4,0,light[3]);
  169.   lmdef(DEFLIGHT,   5,0,light[4]);
  170.   lmdef(DEFLIGHT,   6,0,light[5]);
  171.  
  172.   lmdef(DEFLMODEL,  1,0,NULL);        /* default lighting model */
  173.  
  174.   /* activate the lighting setup */
  175.   lmbind(MATERIAL,1);            /* current material */
  176.   lmbind(LIGHT0,  1);            /* current lights */
  177.   lmbind(LMODEL,  1);            /* current lighting model */
  178.  
  179.   /* create a sphere object */
  180.   object_create();
  181.  
  182.  
  183.   pushmatrix();
  184.     loadmatrix(imat);
  185.     getmatrix(envmat);
  186.   popmatrix();
  187.  
  188.   /* initialize texture mapping */
  189.   if(!getgdesc(GD_TEXTURE)){
  190.       printf("No texture mapping available on this machine\n");
  191.       exit(0);
  192.   } else {
  193.     sbr_tread("texture.rgb", 1);
  194.   }
  195.  
  196. } /* END graphics_init_cb */
  197.  
  198. sbr_bind_high()
  199. {
  200.   lmbind(LIGHT1,  2);
  201.   lmbind(LIGHT2,  3);
  202.   lmbind(LIGHT3,  4);
  203.   lmbind(LIGHT4,  5);
  204.   lmbind(LIGHT5,  6);
  205.   lmbind(LMODEL,  1);                   /* current lighting model */
  206.  
  207. }
  208.  
  209. sbr_bind_low()
  210. {
  211.   lmbind(LIGHT1,  0);
  212.   lmbind(LIGHT2,  0);
  213.   lmbind(LIGHT3,  0);
  214.   lmbind(LIGHT4,  0);
  215.   lmbind(LIGHT5,  0);
  216.   lmbind(LMODEL,  1);                   /* current lighting model */
  217.  
  218. }
  219.  
  220. /******************************************************************************
  221.  *+
  222.  * SCENE_DRAW
  223.  *
  224.  * Function purpose:
  225.  *    Draw the entire scene.
  226.  *
  227.  * Error behaviour:
  228.  *    Returns FALSE upon successful completion of the function call.
  229.  *
  230.  */
  231.  
  232. int scene_draw(void)
  233.  
  234. /*
  235.  *-
  236.  ******************************************************************************
  237.  */
  238.  
  239. {
  240.  
  241.   if( shade_flag == 1){  /* if phong is on */
  242.      sbr_settexture(1);      
  243.      mmode(MTEXTURE);
  244.       loadmatrix(imat);
  245.       translate(.5,.5,0.0);
  246.       scale(.5,.5,1.0);
  247.       multmatrix(mat);
  248.      mmode(MVIEWING);
  249.      }
  250.  
  251.    pushmatrix();
  252.     ortho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
  253.     loadmatrix(imat);
  254.     scale(gr_sca, gr_sca, gr_sca);
  255.     multmatrix(mat);
  256.     czclear(0xff000000, getgdesc(GD_ZMAX));
  257.  
  258.     /* draw a sphere */
  259.     object_draw();
  260.  
  261.    swapbuffers();
  262.   popmatrix();
  263.  
  264.   if( shade_flag == 1 )sbr_settexture(0);    /* turn texturing off */
  265.  
  266.  
  267. } /* END scene_draw */
  268.  
  269.  
  270. sbr_gtfeedb(Matrix matrix,float point[],float result[])
  271. {
  272. register int i;
  273. register float pnt[3];
  274.  
  275. pnt[0]=point[0];pnt[1]=point[1];pnt[2]=point[2];
  276. for(i = 0; i<3; i++)
  277.   result[i] =   pnt[0]*matrix[0][i]+
  278.                 pnt[1]*matrix[1][i]+
  279.                 pnt[2]*matrix[2][i]+
  280.                        matrix[3][i];
  281. } /* end sbr_gtfeedb */
  282.